package com.myosw.dao;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;

import com.myosw.entity.SearchlogShow;
import com.myosw.jdbc.common.QueryHelper;
import com.myosw.pojo.Searchlog;




public class SearchlogDao 
{

	/**
	 * 
	 * @param params touser,fromuser ,time
	 * @return
	 */
	public int[] save(Object[][]  params)
	{
		String sql = "INSERT INTO myosw_searchlog(keyword,time) VALUES(?,?)";
		return QueryHelper.batch(sql , params);
	}
	
	public List<Searchlog> getSearchlogs(String mask) 
	{
		String sql ="SELECT searchlog.keyword " +
				"FROM myosw_searchlog  searchlog " +
				"WHERE searchlog.keyword LIKE ? " +
				" GROUP BY searchlog.keyword" +
				" LIMIT ?,?";
		return  QueryHelper.query(Searchlog.class, sql, "%"+mask+"%",0,10);
		
	}
	
	
	public boolean save(Searchlog searchlog)
	{
		return searchlog.save()>0?true:false;
	}
	
	public List<SearchlogShow> getTop(int from,int to)
	{
		String sql = "SELECT keyword,COUNT(*) as count " +
				"FROM myosw_searchlog  searchlog" +
				" GROUP BY searchlog.keyword" +
				" ORDER BY count DESC " +
				"LIMIT ?,?;";
		return QueryHelper.query(SearchlogShow.class, sql,from,to );
	}
	
	public int getAll(String keyword)
	{
		String sql = "SELECT COUNT(*) as count" +
				" FROM myosw_searchlog  searchlog " +
				"WHERE searchlog.keyword =?";
		return (int) QueryHelper.stat(sql, keyword);
	}
	
	public int getToday(String keyword)
	{
		/**
		 * 用于取得今天的时间
		 */
		Calendar cal =Calendar.getInstance();		
		//cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);	
		Date date = cal.getTime();
		date=DateUtils.setHours(date, 0);
		date=DateUtils.setMinutes(date, 0);
		date=DateUtils.setSeconds(date, 0);
		Timestamp timestamp = new Timestamp(date.getTime());
		String sql = "SELECT COUNT(*) as count FROM myosw_searchlog  searchlog" +
				" WHERE searchlog.keyword =? AND searchlog.time > ?";
		return (int) QueryHelper.stat(sql, keyword,timestamp);
	}
	
	
	public int getYesterday(String keyword)
	{
		/**
		 * 用于取得今天的时间
		 */
		Calendar cal =Calendar.getInstance();		
		//cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);	
		Date date = cal.getTime();
		date=DateUtils.setHours(date, 0);
		date=DateUtils.setMinutes(date, 0);
		date=DateUtils.setSeconds(date, 0);
		Timestamp timestamp = new Timestamp(date.getTime());//今天开始
		date=DateUtils.addDays(date, -1);
		Timestamp ytimestamp = new Timestamp(date.getTime());//昨天开始
		String sql = "SELECT COUNT(*) as count FROM myosw_searchlog  searchlog" +
				" WHERE searchlog.keyword =? AND searchlog.time < ? AND searchlog.time > ?";
		return (int) QueryHelper.stat(sql, keyword,timestamp,ytimestamp);
	}
	
	
	public int getWeek(String keyword)
	{
		/**
		 * 用于取得本周的时间
		 */
		Calendar cal =Calendar.getInstance();		
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);	
		Date date = cal.getTime();
		date=DateUtils.setHours(date, 0);
		date=DateUtils.setMinutes(date, 0);
		date=DateUtils.setSeconds(date, 0);
		Timestamp timestamp = new Timestamp(date.getTime());//今天开始
		
		String sql = "SELECT COUNT(*) as count FROM myosw_searchlog  searchlog" +
				" WHERE searchlog.keyword =? AND  searchlog.time > ?";
		return (int) QueryHelper.stat(sql, keyword,timestamp);
	}
	
	public int getMonth(String keyword)
	{
		/**
		 * 用于取得本月的时间
		 */
		Calendar cal =Calendar.getInstance();		
		cal.set(Calendar.DAY_OF_MONTH, 1);	
		Date date = cal.getTime();
		date=DateUtils.setHours(date, 0);
		date=DateUtils.setMinutes(date, 0);
		date=DateUtils.setSeconds(date, 0);
		Timestamp timestamp = new Timestamp(date.getTime());//今天开始
		
		String sql = "SELECT COUNT(*) as count FROM myosw_searchlog  searchlog" +
				" WHERE searchlog.keyword =? AND  searchlog.time > ?";
		return (int) QueryHelper.stat(sql, keyword,timestamp);
	}
	
	public static void main(String[] args) {
		SearchlogDao searchlogDao = new SearchlogDao();
		Object[][] params={{"中国",null},{"天融信",null}};
		
//		for (int i = 0; i < 10; i++) {
//			searchlogDao.save(params);
//		}
		
		List<SearchlogShow> i=searchlogDao.getTop(0, 10);
		for (SearchlogShow visitlogShow : i) {
			System.out.println(visitlogShow.getKeyword()+"  "+visitlogShow.getCount());
		}
	System.out.println(searchlogDao.getAll("中国"));
	System.out.println(searchlogDao.getToday("中国"));
		System.out.println(searchlogDao.getYesterday("中国"));
		System.out.println(searchlogDao.getWeek("中国"));
		System.out.println(searchlogDao.getMonth("中国"));
	}
	
}
